<?php
/**
 * Created by PhpStorm.
 * User: yyq
 * Date: 2016/12/26
 * Time: 14:53
 */

namespace Advertisement\Controller;


use Common\Controller\AdminbaseController;
use Common\Model\AdsModel;


class ArticleController extends AdminbaseController
{
    private $ads_model;

    public function __construct()
    {
        parent::__construct();
        $this->ads_model = new AdsModel();
    }

    public function artList()
    {
        $artlists = $this->ads_model
            ->field('id, title, content, type, cover_photo, update_time, sort, link')
            ->order('sort asc')
            ->select();
        //dump($artlists);
        $lists = '';
        foreach ($artlists as $k => $v) {
            $v['str_manage'] .= '<a class="" href="' . U('Article/edit', ['id' => $v['id']]) . '">编辑</a>';
            $str = '<img title="点击查看原图" name="' . setUrl($v['cover_photo']) . '" src="' . setUrl($v['cover_photo']) . '?imageView2/2/h/30" onclick="parent.image_preview_dialog(this.name);">';
            $lists .= '<tr>
                <td>' . ($k + 1) . '</td>
                <td>' . $v['id'] . ' </td>
                <td>' . $v['sort'] . ' </td>
                <td>' . $v['title'] . '</td>
                <td>' . $str . '</td>
                <td>' . $v['link'] . '</td>
                <td>' . $v['update_time'] . '</td>
                <td>' . $v['str_manage'] . '</td>
            </tr>';
        }
        $this->assign('lists', $lists);
        $this->display();
    }

    public function add()
    {

        $this->display();
    }

    public function add_post()
    {
        $data['title']          = I('post.post_title');
        $data['content']        = htmlspecialchars_decode(I('post.post_content'));
        $data['create_time']    = getTime();
        $data['update_time']    = getTime();
        $data['type']           = I('post.type');
        $data['cover_photo']    = I('post.cover_photo');
        if (empty($data['title'])) {
            $this->error('标题不能为空');
        }
        if (empty($data['content'])) {
            $this->error('内容不能为空');
        }
        $rst = $this->ads_model->add($data);
        if ($rst) {
            $this->success('', U('artList'));
        } else {
            $this->error();
        }
    }

    public function edit()
    {
        $id = I('get.id');
        $info = $this->ads_model
            ->where(['id' => $id])
            ->find();
//        dump($info);
        $this->assign('info', $info);
        $this->display();
    }

    public function edit_post()
    {
        $id                     = I('post.id');
        $data['title']          = I('post.post_title');
        $data['content']        = htmlspecialchars_decode(I('post.post_content'));
        $data['update_time']    = getTime();
        $data['type']           = I('post.type');
        $data['cover_photo']    = I('post.cover_photo');
        $data['sort']           = I('post.sort');
        $data['link']           = I('post.post_link');
//        dump($data);exit;
        if (empty($data['title'])) {
            $this->error('标题不能为空');
        }
        if (empty($data['content'])) {
            $this->error('内容不能为空');
        }
        $rst = $this->ads_model->where(['id' => $id])->save($data);
        if ($rst) {
            $this->success('', U('artList'));
        } else {
            $this->error();
        }
    }

    public function delete()
    {
        $id = I('get.id');
        $rst = $this->ads_model->where(['id' => $id])->delete();
        if ($rst) {
            $this->success();
        } else {
            $this->error();
        }
    }


}